package com.whoru.logica;

import java.util.ArrayList;

import com.whoru.beans.*;

public class WebServices {
	
	Connection con;
	String loginPath = "http://62.99.17.90/whoru15/whoru_ws/user/login";
	String amigosPath = "http://62.99.17.90/whoru15/whoru_ws/duck/getLagunak";
	String descoPath = "http://62.99.17.90/whoru15/whoru_ws/duck/getNotLagEst";
	String establePath = "http://62.99.17.90/whoru15/whoru_ws/duck/getEstablecimientos";
	String posUpdatePath = "http://62.99.17.90/whoru15/whoru_ws/duck/setLL";

	String noti = "http://62.99.17.90/whoru15/whoru_ws/duck/getNoti";
	String sms = "http://62.99.17.90/whoru15/whoru_ws/duck/getSMS";
	String getuser = "http://62.99.17.90/whoru15/whoru_ws/duck/getUser";
	String setAprove="http://62.99.17.90/whoru15/whoru_ws/duck/setAprove";
	String delAmi= "http://62.99.17.90/whoru15/whoru_ws/duck/delAmi";   

	String onlineUpdatePath = "http://62.99.17.90/whoru15/whoru_ws/duck/setOnline";
	String getMensajePath = "http://62.99.17.90/whoru15/whoru_ws/duck/getStatus";
	String setMensajePath = "http://62.99.17.90/whoru15/whoru_ws/duck/setStatus";

	JsonMe j = new JsonMe();
	
	public WebServices(){
	}
	
	public ArrayList<Notification> getNotificaciones(String uid){
			
			con = new Connection(noti);
	        con.AddParam("uid", uid);
	
	        try {
	        	con.Execute(2);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }  	    
	        
	        return j.trataNotification(con.getResponse());
	}
	
	public String getSms(String rid){
		
		con = new Connection(sms);
        con.AddParam("rid", rid);

        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return j.trataSms(con.getResponse());        
        
	}
	
public String getNombreUsuario(String uid){
		
		con = new Connection(getuser);
        con.AddParam("uid", uid);

        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return j.trataNombreUsuario(con.getResponse());        
        
	}

	public User getUser(String nombre, String pass){
		
		con = new Connection(loginPath);
        con.AddParam("username", nombre);
        con.AddParam("password", pass);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        if (con.getResponse() == "") return new User();
        else return j.trataUser("["+con.getResponse()+"]");
	}
	
	public ArrayList<User> getAmigos(String uid){
		
		con = new Connection(amigosPath);
        con.AddParam("uid", uid);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return j.trataAmigos(con.getResponse());
	}
	
	public ArrayList<User> getEstablecimientos(String uid){
		con = new Connection(establePath);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return j.trataEstablecimientos(con.getResponse());
	}
	
	public ArrayList<User> getDescos(String uid){
		
		con = new Connection(descoPath);
		con.AddParam("uid", uid);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        }  
        return j.trataDescos(con.getResponse());
	}
	
	public void setPosition(Double lat, Double lon, String uid){
		con = new Connection(posUpdatePath);
		
		Double parse;
        parse = lat;
        String lati= parse.toString();
        parse = lon;
        String longi = parse.toString();
		
		con.AddParam("latitude", lati);
        con.AddParam("longitude", longi);
        con.AddParam("uid", uid);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}

	public void setAprove(String requestee, String requester, String rid){
		con = new Connection(setAprove);
		
			
		con.AddParam("requestee_id", requestee);
        con.AddParam("requester_id", requester);
        con.AddParam("rid", rid);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}
	public void cancelAmi( String rid){
		con = new Connection(delAmi);
		
        con.AddParam("rid", rid);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}

	
	public void setOnline(String uid, String data){
		con = new Connection(onlineUpdatePath);
		
		con.AddParam("uid", uid);
		con.AddParam("data", data);
        try {
        	con.Execute(2);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}
	
	public String getMensaje(String uid){
		
		con = new Connection(getMensajePath);
        con.AddParam("uid", uid);
        try {
        	con.Execute(2);
        } catch (Exception e) {e.printStackTrace();}
        
        return j.tratarMensaje(con.getResponse());
	}
	
	public void setMensaje(String uid,String msn){
		
		con = new Connection(setMensajePath);
        con.AddParam("uid", uid);
        con.AddParam("status", msn);
        
        try {
        	con.Execute(2);
        } catch (Exception e) {e.printStackTrace();}


	}


}
